<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" href="http://mashmaker.intel.com/style/settingstyle.css"/>

<script type="text/javascript" src="http://mashmaker.intel.com/v18/mashmaker_api.js"></script>
<script type="text/javascript" src="http://mashmaker.intel.com/v18/mashmaker_settings.js"></script>
<script type="text/javascript" src="http://mashmaker.intel.com/v18/mashmaker_patterns.js"></script>
<script type="text/javascript" src="http://loki.com/javascript/loki.js"></script>

<script type="text/javascript">
//<![CDATA[
// BEGIN: Cookie Utility Functions
// Returns null or unescaped cookie value
function get_cookie ( name ) {
  var results = document.cookie.match ( '(^|;) ?' + name + '=([^;]*)(;|$)' );
  if ( results ) return ( unescape( results[2] ) );
  else return null;
}
// Set cookie value, expiration date is optional.
function set_cookie ( name, value, exp_year, exp_month, exp_day) {
  var cookie_string = name + "=" + escape( value );
  if ( exp_year ) {
    var expires = new Date ( exp_year, exp_month, exp_day );
    cookie_string += "; expires=" + expires.toGMTString();
  }
  document.cookie = cookie_string;
}
// END: Cookie Utility Functions
        
var loki = LokiAPI();

function init(){
    var opts = {
        odd:false, // only one other setting right now
        path:false, // only deal with one instance from whole page
        slots:["name"],
        updateData: updateData,
        updateUi: updateUI
    }
    mashmaker_patterns.itemsByProp.settings("table",opts);

    // Loki integration for location lookup
    if (loki) {
        document.getElementById('loki').innerHTML = "<a href=\"javascript:setLoki();\"><img src=\"http://www.krillion.com/static/images/lokiIcon.png\" width=\"13\" height=\"15\" border=\"0\"></a> <a href=\"javascript:setLoki();\">Locate Me with Loki</a>";
    }
}

function updateUI(){
    document.getElementById("zip").value = global_settings.zip;
}

function updateData(){
    global_settings.zip = document.getElementById("zip").value;
    // Save updated zip in a cookie as user's future default location
    set_cookie("krillion-stockcheck-zip", global_settings.zip, 2020, 1, 1);
}

function setLoki() {
    loki.onSuccess = setLokiSuccess;
    loki.onFailure = setLokiFailure;
    loki.setKey('krillion.com');
    loki.requestLocation(true,loki.LIMITED_STREET_ADDRESS_LOOKUP);
}

function setLokiSuccess(location) {
    var zipField = document.getElementById('zip');
    zipField.value = location.postal_code;
    alert('Press refresh button for new local product results.');
}

function setLokiFailure(errorCode) {
    alert('Location lookup using Loki failed with error code ' +  errorCode + '.');
}
// ]]>
</script>
</head>

<body onload="init()">
    <h1>WhereCamp Test Version</h1>
    <table id="table" class="mm-table">
        <tr class="mm-oddrow">
            <td class="mm-name">Zip Code:</td>
            <td class="mm-val">
                 <input class="mm-input" id="zip" type="text" size="5" name="zip"  value="94041"/>
                 &nbsp;&nbsp;<span id="loki"></span>
            </td>
        </tr>
    </table>
</body>
</html>